#!/usr/bin/env python
# -*- coding:utf-8 -*-
import json
from datetime import datetime, date

from excel.PruTuutDb import PruTuutDb


class MemberAddressObj:
    id: float(0)
    MEMBER_ID: ""
    address_json: ""

    pruTuut_conn = PruTuutDb.get_pool()

    def __init__(self):
        return

    @staticmethod
    def split_params(params):
        g_param = []
        if params['member_id'] is not None and len(params['member_id']) > 0:
            g_param.append('`member_id`=' + "\'" + params['member_id'] + "\'")
        if params['address_json'] is not None and len(params['address_json']) > 0:
            g_param.append('`address_json`=' + "\'" + params['address_json'] + "\'")
        return ', '.join(str(i) for i in g_param)

    @staticmethod
    def concat_datas(g_param):
        arry = []
        address_json = MemberAddressObj.makeAddr(g_param)
        arry.append(g_param['member_id'])
        arry.append(address_json)
        _res = []
        for _val in arry:
            if isinstance(_val, str):
                _res.append(f"\'{_val}\'")
            else:
                if _val is None:
                    _res.append("null")
                else:
                    _res.append(_val)
        return ', '.join(str(i) for i in _res)

    @staticmethod
    def load_db_2_dict(data):
        # `id`,`member_id`,`address_json`
        g_param = {}
        g_param["id"] = data[0]
        g_param["member_id"] = data[1]
        g_param["address_json"] = data[3]
        if data[3] is not None:
            json_obj = json.loads(str(data[3]))
            g_param["address"] = json_obj['address']
            g_param["devaddress"] = json_obj['devaddress']
        else:
            g_param["address"] = {
                "linku": "",
                "linkp": "",
                "post": "",
                "provice": "",
                "city": "",
                "region": "",
                "provice_code": "",
                "city_code": "",
                "region_code": "",
                "detal": "",
            }
            g_param["devaddress"] = {
                "linku": "",
                "linkp": "",
                "post": "",
                "provice": "",
                "city": "",
                "region": "",
                "provice_code": "",
                "city_code": "",
                "region_code": "",
                "detal": "",
            }
        g_param["name"] = data[4]
        g_param["loginName"] = data[5]
        g_param["sex"] = data[6]
        g_param["status"] = data[7]
        return g_param

    # 标记为true时候，从DBA中读取数据，附带ID主键，标记为false时候忽略自增型字段ID
    @staticmethod
    def to_string(with_flg):
        g_param = []
        if with_flg:
            g_param.append("`mem_member_address`.`id`")
            g_param.append("`mem_member_address`.`member_id`")
            g_param.append("`mem_member_address`.`address_json`")
        else:
            g_param.append("`member_id`")
            g_param.append("`address_json`")
        return ', '.join(str(i) for i in g_param)

    @staticmethod
    def str_2_date(input_str) -> object:
        try:
            # 定义日期时间格式化字符串
            format_str = '%Y-%m-%dT%H:%M:%S.%fZ'
            # 将字符串按指定格式转换为datetime对象
            dt_obj = datetime.strptime(input_str, format_str)
            # 提取年、月、日信息并构建Date对象
            year = dt_obj.year
            month = dt_obj.month
            day = dt_obj.day
            date_obj = date(year=year, month=month, day=day)
            return date_obj
        except Exception as e:
            return input_str

    @staticmethod
    def makeAddr(address):
        address_obj = {
            "address": {
                "linku": address["address"]["linku"],
                "linkp": address["address"]["linkp"],
                "post": address["address"]["post"],
                "provice": address["address"]["provice"],
                "city": address["address"]["city"],
                "region": address["address"]["region"],
                "provice_code": address["address"]["provice_code"],
                "city_code": address["address"]["city_code"],
                "region_code": address["address"]["region_code"],
                "detal": address["address"]["detal"]},
            "devaddress": {
                "linku": address["devaddress"]["linku"],
                "linkp": address["devaddress"]["linkp"],
                "post": address["devaddress"]["post"],
                "provice": address["devaddress"]["provice"],
                "city": address["devaddress"]["city"],
                "region": address["devaddress"]["region"],
                "provice_code": address["devaddress"]["provice_code"],
                "city_code": address["devaddress"]["city_code"],
                "region_code": address["devaddress"]["region_code"],
                "detal": address["devaddress"]["detal"]}
        }
        return json.dumps(address_obj, ensure_ascii=False)
